<template>
    <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="6em"
    >
        <el-row type="flex" :gutter="10">
            <el-col :span="24">
                <el-form-item label="上级角色" prop="pid">
                    <el-cascader
                        v-model="formData.pid"
                        :options="roleData"
                        :props="props"
                        clearable
                    ></el-cascader>
                </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item label="角色权限" prop="level_rules">
                    <el-cascader
                        v-model="formData.level_rules"
                        :options="menuData"
                        :props="props2"
                        clearable
                    ></el-cascader>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="角色名称" prop="name">
                    <el-input
                        v-model="formData.name"
                        placeholder="示例：超级管理员"
                        maxlength="10"
                        type="text"
                        clearable
                    ></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="启用状态" prop="status">
                    <el-switch
                        v-model="formData.status"
                        active-value="1"
                        inactive-value="0"
                    ></el-switch>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row type="flex" justify="center">
            <el-button type="primary" icon="el-icon-check" @click="onConfirm"
                >确认</el-button
            >
            <el-button type="default" icon="el-icon-close" @click="onCancel"
                >取消</el-button
            >
        </el-row>
    </el-form>
</template>

<script>
import { post } from "@/assets/js/http.js";

export default {
    name: "role-add",
    props: {
        roleData: {
            type: Array,
            default: () => []
        },
        menuData: {
            type: Array,
            default: () => []
        },
        selection: {
            type: Array,
            default: () => []
        }
    },
    watch: {
        selection() {
            this.setFormData();
        }
    },
    mounted() {
        this.setFormData();
    },
    data() {
        return {
            props: {
                label: "name",
                value: "id",
                children: "children",
                checkStrictly: true
            },
            props2: {
                label: "title",
                value: "id",
                children: "children",
                multiple: true
            },
            formData: {
                pid: "",
                level_rules: [],
                name: "",
                status: "1"
            },
            formRules: {
                level_rules: [
                    {
                        required: true,
                        message: "此项为必填项"
                    }
                ],
                name: [
                    {
                        required: true,
                        message: "此项为必填项"
                    }
                ]
            }
        };
    },
    methods: {
        setFormData() {
            if (this.selection.length === 1) {
                const data = this.selection[0];
                this.formData.pid = data.id;
            }
        },
        onConfirm() {
            this.$refs.formRef.validate(async isValid => {
                if (isValid) {
                    const formData = JSON.parse(JSON.stringify(this.formData));
                    formData.rules = Array.from(
                        new Set(formData.level_rules.flat())
                    );
                    if (Array.isArray(formData.pid)) {
                        formData.pid = formData.pid[formData.pid.length - 1];
                    }
                    await post({
                        url: "/admin/auth.group/add",
                        data: formData,
                        loading: true,
                        successTip: true
                    });
                    this.$emit("update");
                    this.$parent.$emit("update:visible", false);
                    this.$refs.formRef.resetFields();
                }
            });
        },
        onCancel() {
            this.$parent.$emit("update:visible", false);
            this.$refs.formRef.resetFields();
        }
    }
};
</script>
